package pwd

import (
	"errors"
	"github.com/sirupsen/logrus"
	"golang.org/x/crypto/bcrypt"
)

// GenerateHash 密码加密
func GenerateHash(oldPassword string) (string, error) {
	hash, err := bcrypt.GenerateFromPassword([]byte(oldPassword), bcrypt.DefaultCost)
	if err != nil {
		logrus.Error("密码加密失败:", err)
		return "", errors.New("密码加密失败")
	}
	return string(hash), nil
}

// CheckHash 密码解密
func CheckHash(oldPassword, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(oldPassword))
	if err != nil {
		logrus.Error("密码解密失败:", err)
		return false
	}
	return true
}
